//merge PSID data together
use "$temp/psid_hours_wages", clear

******merge on parent identifiers
merge m:1 uniqid using "$temp/psid_parent_identifiers", keep(1 3) nogen //good merge!

******merge on location
merge 1:1 uniqid year using "$temp/psid_location_mobility", keep(1 3) nogen //good merge!

*****merge on childbirth information
merge m:1 uniqid using "$temp/psid_childbirth", keep(1 3) nogen //sensible merge -- not everybody had kids, so we wouldn't expect a full merge

******merge on demographic information and clean up
merge 1:1 uniqid year using "$temp/psid_demographics", keep(1 3) nogen
sort uniqid year

//fill in sex information for non-survey years
replace sex = sex[_n+1] if sex[_n] == . & sex[_n+1]!=. & uniqid[_n] == uniqid[_n+1]
replace sex = sex[_n-1] if sex[_n] == . & sex[_n-1]!=. & uniqid[_n] == uniqid[_n-1]

//fill in sex information for non-survey years
replace race = race[_n+1] if race[_n] == . & race[_n+1]!=. & uniqid[_n] == uniqid[_n+1]
replace race = race[_n-1] if race[_n] == . & race[_n-1]!=. & uniqid[_n] == uniqid[_n-1]

//note: race is missing for kids currently, but that's OK because we're really only interested in household
//heads and spouses anyway

//update age
replace age = age[_n+1] - 1 if age[_n] == . & age[_n+1]!=. & uniqid[_n] == uniqid[_n+1] 
replace age = age[_n-1] + 1 if age[_n] == . & age[_n-1]!=. & uniqid[_n] == uniqid[_n-1]

//update education. If education before and after year are the same, then fill in with whatever value is there
replace educ = educ[_n+1] if educ[_n] == . & educ[_n+1]!=. & educ[_n+1] == educ[_n-1] & uniqid[_n] == uniqid[_n+1] & uniqid[_n] == uniqid[_n-1]

replace educ = educ[_n+1]-1 if educ[_n] == . & educ[_n+1]!=. & educ[_n+1] != educ[_n-1] & uniqid[_n] == uniqid[_n+1] & uniqid[_n] == uniqid[_n-1]

//fill in first and last-observation education, assuming same as next year
replace educ = educ[_n+1] if uniqid[_n] == uniqid[_n+1] & uniqid[_n]!=uniqid[_n-1] & educ[_n] == . & educ[_n+1]!=.
replace educ = educ[_n-1] if uniqid[_n] == uniqid[_n-1] & uniqid[_n]!=uniqid[_n+1] & educ[_n] == . & educ[_n+1]!=.

//now have all we need to begin constructing analysis sample!
save "$temp/psid_master", replace




//end of do-file